package com.jsgt.greekfire.ui.mainassets

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.fhx.core.base.BaseActivity
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.ConvertConfigBean
import com.jsgt.greekfire.common.util.RecycleEmptyView
import com.jsgt.greekfire.factory.CoinViewModelFactory
import com.jsgt.greekfire.ui.mainassets.adapter.ConvertConfigAdapter
import com.jsgt.greekfire.viewmodel.CoinViewModel
import com.scwang.smartrefresh.header.MaterialHeader
import kotlinx.android.synthetic.main.activity_convert_config_list.*
import kotlinx.android.synthetic.main.view_title.*

class ConvertConfigListActivity : BaseActivity() {
    private var symbol: String? = null
    var convertConfigList: ArrayList<ConvertConfigBean.ToAssetListBean>? = ArrayList()
    private val coinViewModel: CoinViewModel by lazy {
        ViewModelProvider(
            this,
            CoinViewModelFactory()
        ).get(CoinViewModel::class.java)
    }

    private val convertConfigAdapter by lazy {
        ConvertConfigAdapter(this, R.layout.item_convert_config_list)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        symbol = intent.getStringExtra("symbol")
    }

    override fun getLayoutId(): Int = R.layout.activity_convert_config_list

    override fun main() {
        refresh.setRefreshHeader(MaterialHeader(this));
        title_tv?.text = resources.getString(R.string.currency_selection_title)
        recycler.setLayoutManager(LinearLayoutManager(this, RecyclerView.VERTICAL, false))
        recycler.adapter = convertConfigAdapter
        convertConfigAdapter.setOnItemClickListener { adapter, view, position ->
            val intent = Intent()
            intent.putExtra("currentSymbol", convertConfigAdapter.data.get(position).coin)
            intent.putExtra("assetGuid", convertConfigAdapter.data.get(position).assetId.toString())
            setResult(RESULT_OK, intent)
            finish()
        }

        refresh.autoRefresh()
        refresh.setOnRefreshListener {
            coinViewModel.convertConfigList()
        }
        coinViewModel.convertConfigData.observe(this, Observer {
            if (it.result != null) {
                convertConfigList!!.clear()
                if (TextUtils.isEmpty(symbol)) {
                    for (index in it.result) {
                        var assetList = ConvertConfigBean.ToAssetListBean()
                        assetList.coin = index.coin
                        assetList.assetId = index.fromAssetId
                        convertConfigList!!.add(assetList)
                    }
                } else {
                    for (index in it.result) {
                        if (index.coin.equals(symbol)) {
                            index.toAssetList?.let { it1 -> convertConfigList!!.addAll(it1) }
                        }
                    }
                }
                convertConfigAdapter.setNewData(convertConfigList)
                if (convertConfigList!!.size <= 0) {
                    RecycleEmptyView.showEmptyView(
                        this,
                        convertConfigAdapter,
                        "暂无币种",
                        R.mipmap.icon_empty_no_data
                    )
                }
                refresh.finishLoadMoreWithNoMoreData();
                refresh.finishLoadMore();
            }
        })
        coinViewModel.errorLiveData.observe(this, Observer {
            showToast(it.toString())
        })
    }

}